package com.study.pdfbox;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.File;
import java.io.IOException;

/**
 * @author xl-9527
 * @since 2025/8/4
 **/
public class EditPdf {

    private static final String PDF_FILE = "/Users/a1-6/Desktop/project/idea/back/common-study-demo/study-lib/study-lib-pdf-box/src/main/resources/pdf/1.pdf";
    private static final String FONT_FILE = "/Users/a1-6/Desktop/project/idea/back/common-study-demo/study-lib/study-lib-pdf-box/src/main/resources/font/wryh.ttf";
    private static final String PDF_FILE_NEW = "/Users/a1-6/Desktop/project/idea/back/common-study-demo/study-lib/study-lib-pdf-box/src/main/resources/pdf/1-1.pdf";

    public static void main(String[] args) throws IOException {
        try (
                final PDDocument pdDocument = Loader.loadPDF(new File(PDF_FILE))
        ) {
            final PDType0Font font = PDType0Font.load(pdDocument, new File(FONT_FILE));
            final PDPageContentStream stream = new PDPageContentStream(pdDocument, pdDocument.getPage(0), PDPageContentStream.AppendMode.APPEND, true, true);
            stream.beginText();
            stream.newLineAtOffset(50, 50);
            stream.setFont(font, 8);
            stream.showText("邮袋号：90 #01");
            stream.endText();
            stream.close();
            pdDocument.save(PDF_FILE_NEW);
        }
    }
}
